package com.gitee.wsl.compose.ui.search

import androidx.compose.animation.Crossfade
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.gitee.wsl.compose.ext.flow.rememberFlow
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow

@Composable
fun <T> SearchMovieScreen(
    searchStateFlow: Flow<SearchState<T>>,
    onMovieClick: (path: String) -> Unit,
    search:(selectText:String)->Unit,
    idle:@Composable ()->Unit = { Idle() },
    error:@Composable ()->Unit = { Error() },
    loading:@Composable ()->Unit={ Loading() },
    container:@Composable ()->Unit,
) {

    val stateLifecycleAware = rememberFlow(searchStateFlow)
    val state by stateLifecycleAware.collectAsState(SearchState.initial())

    val (movieTitle, setMovieTitle) = rememberSaveable { mutableStateOf("") }

    LaunchedEffect(movieTitle) {
        delay(700)
        if (movieTitle.isNotBlank())
            search(movieTitle)
    }

    Scaffold(
        topBar = {
            Surface(shadowElevation = 4.dp) {
                Box(
                    modifier = Modifier
                        .statusBarsPadding()
                        .padding(vertical = 8.dp, horizontal = 4.dp)
                ) {
                    SearchInput(
                        modifier = Modifier.fillMaxWidth(),
                        placeholder = {
                            //Text(text = stringResource(R.string.search_movie_input_placeholder))
                        },
                        value = movieTitle,
                        onValueChange = setMovieTitle
                    )
                }
            }
        }
    ) { padding ->

        Box(
            modifier = Modifier
                .navigationBarsPadding()
                .padding(padding)
                .fillMaxSize()
        ) {

            Crossfade(targetState = state) { state ->
                when {

                    !state.isLoading && !state.hasAnErrorOccurred && state.result.isEmpty() -> idle()

                    state.isLoading -> loading()

                    !state.isLoading && !state.hasAnErrorOccurred && state.result.isNotEmpty() -> {
                        container()
                        /*Movies(
                            movies = state.result,
                            onMovieClick = { movieItem ->
                                val moviePath = movieItem.url.substringAfterLast("/")
                                onMovieClick(moviePath)
                            }
                        )*/
                    }

                    state.hasAnErrorOccurred -> error()
                }
            }
        }

    }
}

@Composable
private fun Idle() {
    Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
        /*Image(
            painter = painterResource( R.drawable.ils_search),
            contentDescription = null
        )*/
    }
}

@Composable
private fun Error() {

    Box(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {
        //Text(text = stringResource(R.string.error_text))
    }
}

@Composable
fun Loading() {
    Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
        CircularProgressIndicator(
            modifier = Modifier.align(
                Alignment.Center
            )
        )
    }
}


@Immutable
data class SearchState<T>(
    val isLoading: Boolean = false,
    val result: Map<String, List<T>>,
    val hasAnErrorOccurred: Boolean
) {
    companion object {
        fun <T> initial() = SearchState<T>(
            isLoading = false,
            result = emptyMap(),
            hasAnErrorOccurred = false
        )
    }
}
